

package com.hazelcast.internal.dynamicconfig;

import com.hazelcast.config.JavaSerializationFilterConfig;
import com.hazelcast.config.UserCodeNamespaceConfig;
import com.hazelcast.config.UserCodeNamespacesConfig;

import javax.annotation.Nullable;
import java.util.function.Supplier;

/**
 * {@link UserCodeNamespacesConfig} wrapper that supports dynamically updating
 */
public class DynamicUserCodeNamespacesConfig extends UserCodeNamespacesConfig {
    /**
     * The configuration service can change at runtime, so we always need to grab a fresh copy
     */
    private final Supplier<ConfigurationService> configurationServiceAccessor;

    public DynamicUserCodeNamespacesConfig(Supplier<ConfigurationService> configurationServiceAccessor, UserCodeNamespacesConfig userCodeNamespacesConfig) {
        super(userCodeNamespacesConfig);
        this.configurationServiceAccessor = configurationServiceAccessor;
    }

    @Override
    public UserCodeNamespacesConfig addNamespaceConfig(UserCodeNamespaceConfig userCodeNamespaceConfig) {
        if (!isEnabled()) {
            throw new UnsupportedOperationException("Cannot add namespace while Namespaces are disabled.");
        }
        super.addNamespaceConfig(userCodeNamespaceConfig);
        configurationServiceAccessor.get().broadcastConfig(userCodeNamespaceConfig);
        return this;
    }

    @Override
    public UserCodeNamespacesConfig removeNamespaceConfig(String namespace) {
        throw new UnsupportedOperationException("Namespaces cannot be removed at runtime");
    }

    @Override
    public void setClassFilterConfig(@Nullable JavaSerializationFilterConfig classFilterConfig) {
        throw new UnsupportedOperationException("Cannot define JavaSerializationFilterConfig for NamespacesConfig at runtime.");
    }

    @Override
    public UserCodeNamespacesConfig setEnabled(boolean enabled) {
        throw new UnsupportedOperationException("Cannot enable or disable NamespacesConfig at runtime.");
    }
}
